<?php
/**
 * 教师列表管理
 * @author a
 *
 */
class Teacher extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('teacher_model');
        $this->load->model('teacher_work_time_model');
        $this->load->model('nursery_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    /**
     * 列表
     */
    public function Index(){
        try {
            $teacher_list = $this->teacher_model->findAll(array('status'=>1),'*','weight desc',$this->offse,$this->page_size);
            if(empty($teacher_list))throw new Exception('没有记录');
            
            //获取园所名称
            $where = "id IN (". filterValue($teacher_list,'nursery_id',true) .")";
            $nursery_list = $this->nursery_model->findAll($where,'id,title as nursery_title');
            $nursery_list = restByKey($nursery_list,'id');
            $teacher_list = arrayMerge($teacher_list,'nursery_id',$nursery_list);	//合并
            
            $total = $this->teacher_model->findCount(array('status'=>1)); //总数
            $pagelink = $this->pagination($total,$this->page_size);

        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'admin/teacher/index', array(
            'pagelink'     =>  $pagelink,
            'teacher_list'    =>  $teacher_list,
            'error'     =>  empty($error) ? '' : $error,
        ), false);
        $this->t->render();
    }
    /**
     * 修改排序
     */
    public function sort(){
        try {
            $return = array('code'=>10,'msg'=>'修改成功');
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('ID为空');
            $sort = intval($this->input->get('sort'));
            
            $bool = $this->teacher_model->update(array('weight'=>$sort),array('id'=>$id));
            
            if(empty($bool))throw new Exception('修改失败');
            
        }catch (Exception $e){
            $return['code'] = 11;
            $return['msg'] = $e->getMessage();
        }
        echo json_encode($return);
    }
    /**
     * 修改
     */ 
    public function Update(){
        try {
            $id = intval($_REQUEST['id']);
            if(empty($id))throw new Exception('ID为空');
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
                $data = array();
                $data['name'] = $this->input->post('name');
                $data['mobile'] = $this->input->post('mobile');
                $data['explain'] = $this->input->post('explain');
                $data['sex'] = intval($this->input->post('sex'));
                $data['nursery_id'] = intval($this->input->post('nursery_id'));
                $data['book_num'] = intval($this->input->post('book_num'));
                $data['update_date'] = date('Y-m-d H:i:s');
                $bool = $this->teacher_model->update($data,array('id'=>$id));
                if(empty($bool))throw new Exception('修改失败');
                redirect("/admin/teacher");
            }

            $teacher_info = $this->teacher_model->find(array('id'=>$id));
            
            $nursery_list = $this->nursery_model->findAll(array('status'=>1),'id,title');
            
            $this->t->write_view('content', 'admin/teacher/update', array(
                'teacher_info' =>  $teacher_info,
            	'nursery_list'	=>	$nursery_list,
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/teacher/',
            ), false);
            $this->t->render();
        }
    }
    /**
     * 添加
     */
    public function Add(){
        try {
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
                $data = array();
                $data['nursery_id'] = intval($this->input->post('nursery_id'));
                $data['name'] = $this->input->post('name');
                $data['mobile'] = $this->input->post('mobile');
                $data['explain'] = $this->input->post('explain');
                $data['sex'] = intval($this->input->post('sex'));
                $data['book_num'] = intval($this->input->post('book_num'));
                $data['create_date'] = date('Y-m-d H:i:s');
                $bool = $this->teacher_model->insert($data);
                if(empty($bool))throw new Exception('添加失败');
                redirect("/admin/teacher");
            }
            
            $nursery_list = $this->nursery_model->findAll(array('status'=>1),'id,title');
            
            $this->t->write_view('content', 'admin/teacher/add', array(
            		'nursery_list'	=>	$nursery_list,
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/teacher/',
            ), false);
            $this->t->render();
        }
    }
    /**
     * 删除
     */
    public function Delete(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('ID为空');
        
            $bool = $this->teacher_model->update(array('status'=>0),array('id'=>$id));
            if(empty($bool))throw new Exception('删除失败');
        
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/teacher/',
            ), false);
            $this->t->render();            
        }
        redirect("/admin/teacher");
    }
    /**
     * 修改时间
     */
    public function Updatetime(){
        try {
        	$id = intval($_REQUEST['id']);
        	if(empty($id))throw new Exception('ID为空');
        	
        	//获取时间信息
        	$teacher_work_time_list = $this->teacher_work_time_model->findAll(array('teacher_id'=>$id));
        	if(!empty($teacher_work_time_list)){
        		$tmp = array();
        		foreach ($teacher_work_time_list as $key=>$val){
        			$tmp[$val['work_week']][strtotime($val['work_time_start']).'-'.strtotime($val['work_time_end'])] = $val;
        		}
        		$teacher_work_time_list = $tmp;
        	}
        	//echo '<pre>';print_r($teacher_work_time_list);die();
        	
        	$this->t->write_view('content', 'admin/teacher/updatetime', array(
        			'teacher_work_time_list'	=>	$teacher_work_time_list,
        			'id'	=>	$id,
        			'type_name'	=>	array('周日','周一','周二','周三','周四','周五','周六'),
        	), false);
        	$this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/teacher/',
            ), false);
            $this->t->render();
        }
    }
    /**
     * 修改时间ajax
     */
    public function Updatetimeajax(){
    	try {
    		$return = array('code'=>10,'msg'=>'OK','id'=>0);
    		$checked = $this->input->get('checked');
    		$teacher_id = intval($this->input->get('teacher_id'));
    		if(empty($teacher_id))throw new Exception('教师ID为空');
    		$id = intval($this->input->get('id'));	//	存在则修改
    		$work_week = intval($this->input->get('work_week'));
    		$time_data = $this->input->get('time_data');
    		if(empty($time_data))throw new Exception('时间为空');
    		$time_data = explode('-',$time_data);
    		$data = array();
    		$data['teacher_id'] = $teacher_id;
    		$data['work_week'] = $work_week;
    		$data['work_time_start'] = $time_data[0];
    		$data['work_time_end'] = $time_data[1];
    		$data['status'] = $checked == 'true' ? 1 : 0;
    		if(!empty($id)){	//修改
    			$data['update_date'] = date('Y-m-d H:i:s');
    			$bool = $this->teacher_work_time_model->update($data,array('id'=>$id));
    			if(empty($bool))throw new Exception('失败');
    			$return['id'] = $id;
    		}else{		//添加
    			$data['create_date'] = date('Y-m-d H:i:s');
    			$bool = $this->teacher_work_time_model->insert($data);
    			if(empty($bool))throw new Exception('失败');
    			$return['id'] = $this->teacher_work_time_model->getInsertID();
    		}
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);exit();
    }
    
    
    
    
    
    
    
    
    
    
}






























